The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Use the 使用JColorChooser
class to enable users to choose from a palette of colors. JColorChooser
类,用户可以从调色板中选择颜色。A color chooser is a component that you can place anywhere within your program GUI. 颜色选择器是一个可以放置在程序GUI中任何位置的组件。The JColorChooser
API also makes it easy to bring up a dialog (modal or not) that contains a color chooser.JColorChooser
API还可以轻松地打开包含颜色选择器的对话框(模式或非模式)。
Here is a picture of an application that uses a color chooser to set the text color in a banner:下面是一个应用程序的图片,该应用程序使用颜色选择器设置横幅中的文本颜色:
The source code for the program is in 该程序的源代码位于ColorChooserDemo.java
.ColorChooserDemo.java
中。
The color chooser consists of everything within the box labeled Choose Text Color. 颜色选择器由标记为“选择文本颜色”的框中的所有内容组成。This is what a standard color chooser looks like in the Java Look & Feel. 这就是Java外观中标准颜色选择器的外观。It contains two parts, a tabbed pane and a preview panel. 它包含两个部分,一个选项卡式窗格和一个预览面板。The three tabs in the tabbed pane select chooser panels. 选项卡窗格中的三个选项卡选择选择器面板。The preview panel below the tabbed pane displays the currently selected color.选项卡窗格下方的预览面板显示当前选定的颜色。
Here is the code from the example that creates a 下面是创建JColorChooser
instance and adds it to a container:JColorChooser
实例并将其添加到容器的示例代码:
public class ColorChooserDemo extends JPanel ... { public ColorChooserDemo() { super(new BorderLayout()); banner = new JLabel("Welcome to the Tutorial Zone!", JLabel.CENTER); banner.setForeground(Color.yellow); . . . tcc = new JColorChooser(banner.getForeground()); . . . add(tcc, BorderLayout.PAGE_END); }
The 上一个代码片段中的JColorChooser
constructor in the previous code snippet takes a Color
argument, which specifies the chooser's initially selected color. JColorChooser
构造函数采用一个颜色参数,该参数指定选择器最初选择的颜色。If you do not specify the initial color, then the color chooser displays 如果未指定初始颜色,则颜色选择器将显示Color.white
. Color.white
。See the 有关可以使用的颜色常数列表,请参阅Color
API documentation for a list of color constants you can use.Color
API文档。
A color chooser uses an instance of 颜色选择器使用ColorSelectionModel
to contain and manage the current selection. ColorSelectionModel
的实例来包含和管理当前选择。The color selection model fires a change event whenever the user changes the color in the color chooser. 每当用户在颜色选择器中更改颜色时,颜色选择模型将触发更改事件。The example program registers a change listener with the color selection model so that it can update the banner at the top of the window. 示例程序向颜色选择模型注册一个更改侦听器,以便它可以更新窗口顶部的横幅。The following code registers and implements the change listener:以下代码注册并实现更改侦听器:
tcc.getSelectionModel().addChangeListener(this); . . . public void stateChanged(ChangeEvent e) { Color newColor = tcc.getColor(); banner.setForeground(newColor); }
See How to Write a Change Listener for general information about change listeners and change events.有关更改侦听器和更改事件的一般信息,请参阅如何编写更改侦听器。
A basic color chooser, like the one used in the example program, is sufficient for many programs. 一个基本的颜色选择器,如示例程序中使用的,对于许多程序来说都是足够的。However, the color chooser API allows you to customize a color chooser by providing it with a preview panel of your own design, by adding your own chooser panels to it, or by removing existing chooser panels from the color chooser. 但是,颜色选择器API允许您通过为颜色选择器提供自己设计的预览面板、向其添加自己的选择器面板或从颜色选择器中删除现有选择器面板来自定义颜色选择器。Additionally, the 此外,JColorChooser
class provides two methods that make it easy to use a color chooser within a dialog.JColorChooser
类提供了两种方法,可以轻松地在对话框中使用颜色选择器。
The rest of this section discusses these topics:本节其余部分将讨论以下主题:
Now let's turn our attention to ColorChooserDemo2, a modified version of the previous demo program that uses more of the 现在让我们将注意力转向ColorChooserDemo2,它是先前演示程序的修改版本,使用了更多的JColorChooser
API. JColorChooser
API。
Here is a picture of ColorChooserDemo2:以下是ColorChooserDemo2的图片:
This program customizes the banner text color chooser in these ways:此程序通过以下方式自定义横幅文本颜色选择器:
Removing or Replacing the Preview Panel删除或替换预览面板 covers the first customization. 涵盖第一个定制。Creating a Custom Chooser Panel创建自定义选择器面板 discusses the last two.讨论后两者。
This program also adds a button that brings up a color chooser in a dialog, which you can use to set the banner background color.该程序还添加了一个按钮,用于在对话框中打开颜色选择器,您可以使用该按钮设置横幅背景颜色。
The JColorChooser
class provides two class methods to make it easy to use a color chooser in a dialog. JColorChooser
类提供了两个类方法,以便于在对话框中使用颜色选择器。ColorChooserDemo2 uses one of these methods, ColorChooserDemo2使用其中一种方法showDialog
, to display the background color chooser when the user clicks the Show Color Chooser... button. showDialog
,在用户单击“显示颜色选择器…”按钮时显示背景颜色选择器。Here is the single line of code from the example that brings up the background color chooser in a dialog:下面是示例中的一行代码,在对话框中显示背景颜色选择器:
Color newColor = JColorChooser.showDialog( ColorChooserDemo2.this, "Choose Background Color", banner.getBackground());
The first argument is the parent for the dialog, the second is the dialog title, and the third is the initially selected color.第一个参数是对话框的父参数,第二个是对话框标题,第三个是最初选择的颜色。
The dialog disappears under three conditions: the user chooses a color and clicks the OK button, the user cancels the operation with the Cancel button, or the user dismisses the dialog with a frame control. 对话框在三种情况下消失:用户选择颜色并单击“确定”按钮,用户使用“取消”按钮取消操作,或用户使用框架控件取消对话框。If the user chooses a color, the 如果用户选择颜色,showDialog
method returns the new color. showDialog
方法将返回新颜色。If the user cancels the operation or dismisses the window, the method returns 如果用户取消操作或关闭窗口,该方法将返回null
. null
。Here is the code from the example that updates the banner background color according to the value returned by 下面是根据showDialog
:showDialog
返回的值更新横幅背景颜色的示例代码:
if (newColor != null) { banner.setBackground(newColor); }
The dialog created by showDialog
is modal. showDialog
创建的对话框是模态对话框。If you want a non-modal dialog, you can use 如果需要非模态对话框,可以使用JColorChooser
's createDialog
method to create the dialog. JColorChooser
的createDialog
方法来创建对话框。This method also lets you specify action listeners for the OK and Cancel buttons in the dialog window. 此方法还允许您为对话框窗口中的“确定”和“取消”按钮指定操作侦听器。Use 使用JDialog
's show
method to display the dialog created by this method. JDialog
的show
方法显示由该方法创建的对话框。For an example that uses this method, see Specifying Other Editors in the How to Use Tables section.有关使用此方法的示例,请参阅“如何使用表格”部分中的“指定其他编辑器”。
By default, the color chooser displays a preview panel. 默认情况下,颜色选择器显示预览面板。ColorChooserDemo2 removes the text color chooser's preview panel with this line of code:ColorChooserDemo2
使用以下代码行删除文本颜色选择器的预览面板:
tcc.setPreviewPanel(new JPanel());
This effectively removes the preview panel because a plain 这有效地删除了预览面板,因为普通JPanel
has no size and no default view. JPanel
没有大小,也没有默认视图。To set the preview panel back to the default, use 要将预览面板设置回默认值,请使用null
as the argument to setPreviewPanel
.null
作为setPreviewPanel
的参数。
To provide a custom preview panel, you also use 要提供自定义预览面板,还可以使用setPreviewPanel
. setPreviewPanel
。The component you pass into the method should inherit from 传递到方法中的组件应该继承JComponent
, specify a reasonable size, and provide a customized view of the current color. JComponent
,指定合理的大小,并提供当前颜色的自定义视图。To get notified when the user changes the color in the color chooser, the preview panel must register as a change listener on the color chooser's color selection model as described previously.要在用户更改颜色选择器中的颜色时获得通知,预览面板必须注册为颜色选择器的颜色选择模型上的更改侦听器,如前所述。
The default color chooser provides five chooser panels:默认颜色选择器提供五个选择器面板:
You can extend the default color chooser by adding chooser panels of your own design with 可以通过使用addChooserPanel
, or you can limit it by removing chooser panels with removeChooserPanel
.addChooserPanel
添加自己设计的选择器面板来扩展默认颜色选择器,也可以通过使用removeChooserPanel
删除选择器面板来限制默认颜色选择器。
If you want to remove all of the default chooser panels and add one or more of your own, you can do this with a single call to 如果要删除所有默认选择器面板并添加一个或多个自己的面板,可以通过调用setChooserPanels
. setChooserPanels
来完成此操作。ColorChooserDemo2 uses this method to replace the default chooser panels with an instance of 使用此方法将默认选择器面板替换为自定义选择器面板CrayonPanel
, a custom chooser panel. CrayonPanel
的实例。Here is the call to 下面是该示例中对setChooserPanels
from that example:setChooserPanels
的调用:
//Override the chooser panels with our own. AbstractColorChooserPanel panels[] = { new CrayonPanel() }; tcc.setChooserPanels(panels);
The code is straightforward: it creates an array containing the 代码很简单:它创建了一个包含CrayonPanel
. CrayonPanel
的数组。Next the code calls 接下来,代码调用setChooserPanels
to set the contents of the array as the color chooser's chooser panels.setChooserPanels
,将数组的内容设置为颜色选择器的选择器面板。
CrayonPanel
is a subclass of 是AbstractColorChooserPanel
and overrides the five abstract methods defined in its superclass:AbstractColorChooserPanel
的子类,并重写其超类中定义的五个抽象方法:
void buildChooser()
void updateChooser()
public void updateChooser() { Color color = getColorFromModel(); if (Color.red.equals(color)) { redCrayon.setSelected(true); } else if (Color.yellow.equals(color)) { yellowCrayon.setSelected(true); } else if (Color.green.equals(color)) { greenCrayon.setSelected(true); } else if (Color.blue.equals(color)) { blueCrayon.setSelected(true); } }
String getDisplayName()
getDisplayName
method:getDisplayName
方法:
public String getDisplayName() { return "Crayons"; }
Icon getSmallDisplayIcon()
null
.null
。Icon getLargeDisplayIcon()
null
.null
。The following tables list the commonly used 下表列出了常用的JColorChooser
constructors and methods. JColorChooser
构造函数和方法。Other methods you might call are listed in the API tables in The JComponent Class. 您可能调用的其他方法在JComponent类的API表中列出。The API for using color choosers falls into these categories:使用颜色选择器的API分为以下几类:
JColorChooser() JColorChooser(Color) JColorChooser(ColorSelectionModel)
|
Color.white . Color.white 的颜色选择器。ColorSelectionModel argument, when present, provides the color chooser with a color selection model.ColorSelectionModel 参数(如果存在)为颜色选择器提供颜色选择模型。 |
Color showDialog(Component, String, Color) |
Component argument is the parent of the dialog, the String argument specifies the dialog title, and the Color argument specifies the chooser's initial color.Component 参数是对话框的父级,String 参数指定对话框标题,Color 参数指定选择器的初始颜色。 |
JDialog createDialog(Component, String, |
showDialog , the Component argument is the parent of the dialog and the String argument specifies the dialog title. showDialog 一样,Component 参数是对话框的父级,String 参数指定对话框标题。boolean specifies whether the dialog is modal, the JColorChooser is the color chooser to display in the dialog, the first ActionListener is for the OK button, and the second is for the Cancel button.boolean 指定对话框是否为模态,JColorChooser 是对话框中显示的颜色选择器,第一个ActionListener 用于“确定”按钮,第二个用于“取消”按钮。 |
void setPreviewPanel(JComponent) JComponent getPreviewPanel() |
new JPanel() as an argument. new JPanel() 作为参数。null .null 。 |
void setChooserPanels(AbstractColorChooserPanel[]) AbstractColorChooserPanel[] getChooserPanels() |
|
void addChooserPanel(AbstractColorChooserPanel) AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel) |
|
void setDragEnabled(boolean) boolean getDragEnabled() |
dragEnabled property, which must be true to enable drag handling on this component. dragEnabled 属性,该属性必须为true 才能在此组件上启用拖动处理。false 。 |
void setColor(Color) void setColor(int, int, int) void setColor(int) Color getColor() |
setColor method interprets the three integers together as an RGB color. setColor 方法的三个整数版本将三个整数一起解释为RGB颜色。setColor method divides the integer into four 8-bit bytes and interprets the integer as an RGB color as follows:setColor 方法的单整数版本将整数分成四个8位字节,并将整数解释为RGB颜色,如下所示:
![]() |
void setSelectionModel(ColorSelectionModel) ColorSelectionModel getSelectionModel() |
This table shows the examples that use 下表显示了使用JColorChooser
and where those examples are described.JColorChooser
的示例以及这些示例的描述位置。
ColorChooserDemo |
||
ColorChooserDemo2 |
showDialog .showDialog 创建的对话框中使用一个自定义颜色选择器和一个标准颜色选择器。 | |
TableDialogEditDemo |
How to Use Tables |
createDialog .createDialog 创建的。 |
BasicDnD |